package com.huawei.secure.android.common.util;

import android.text.TextUtils;
import android.util.Log;
import java.io.UnsupportedEncodingException;
import java.util.Locale;

public final class HexUtil {
    private static final String TAG = "HexUtil";
    private static final String h = "";

    private HexUtil() {
    }

    public static String byteArray2HexStr(byte[] bArr) {
        if (bArr == null || bArr.length == 0) {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (byte b : bArr) {
            String toHexString = Integer.toHexString(b & 255);
            if (toHexString.length() == 1) {
                stringBuilder.append('0');
            }
            stringBuilder.append(toHexString);
        }
        return stringBuilder.toString();
    }

    public static String byteArray2HexStr(String str) {
        String str2 = "";
        if (!TextUtils.isEmpty(str)) {
            try {
                str2 = byteArray2HexStr(str.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                Log.e(TAG, "byte array 2 hex string UnsupportedEncodingException : " + e.getMessage());
            } catch (Exception e2) {
                Log.e(TAG, "byte array 2 hex string exception : " + e2.getMessage());
            }
        }
        return str2;
    }

    public static byte[] hexStr2ByteArray(String str) {
        Exception e;
        int i = 0;
        if (TextUtils.isEmpty(str)) {
            return new byte[0];
        }
        String toUpperCase = str.toUpperCase(Locale.ENGLISH);
        byte[] bArr = new byte[(toUpperCase.length() / 2)];
        try {
            byte[] bytes = toUpperCase.getBytes("UTF-8");
            while (i < bArr.length) {
                bArr[i] = (byte) (((byte) (Byte.decode("0x" + new String(new byte[]{bytes[i * 2]}, "UTF-8")).byteValue() << 4)) ^ Byte.decode("0x" + new String(new byte[]{bytes[(i * 2) + 1]}, "UTF-8")).byteValue());
                i++;
            }
        } catch (UnsupportedEncodingException e2) {
            e = e2;
            Log.e(TAG, "hex string 2 byte UnsupportedEncodingException or NumberFormatException : " + e.getMessage());
        } catch (NumberFormatException e3) {
            e = e3;
            Log.e(TAG, "hex string 2 byte UnsupportedEncodingException or NumberFormatException : " + e.getMessage());
        } catch (Exception e4) {
            Log.e(TAG, "byte array 2 hex string exception : " + e4.getMessage());
        }
        return bArr;
    }
}
